package Gui;
import java.awt.Color;
import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.security.Timestamp;
import java.util.Date;

import java.awt.EventQueue;

import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTextField;



import com.toedter.calendar.JDateChooser;

import Entity.Customer;
import Entity.History;
import controller.CreateListControl;
import controller.HistoryControl;
import java.awt.SystemColor;
import javax.swing.event.AncestorListener;
import javax.swing.event.AncestorEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;

import lu.tudor.santec.jtimechooser.JTimeChooser;

// TODO: Auto-generated Javadoc
/**
 * The Class UpdateHistory.
 */
public class UpdateHistory extends JPanel{
	
	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = -6400283674228776823L;
	
	/** The Customer id label. */
	public static JLabel CustomerIDLabel;
	
	/** The Deal id label. */
	public static JLabel DealIDLabel;
	
	/** The Date label. */
	public static JLabel DateLabel;
	
	/** The Documentation label. */
	public static JLabel DocumentationLabel;
	
	/** The Order label. */
	public static JLabel OrderLabel;
	
	/** The Customer id text field. */
	public static JTextField CustomerIDTextField;
	
	/** The Deal id text field. */
	public static JTextField DealIDTextField;
	
	/** The calendar button1. */
	public static JDateChooser calendarButton1;
	
	/** The time button1. */
	public static JTimeChooser timeButton1;
	
	/** The Addbutton. */
	public static JButton Addbutton;
	
	/** The confirmation lable. */
	public static JLabel confirmationLable=null;
	
	private mainf mf;
	
	/** The back button. */
	public static JButton backButton = new JButton("Back");
	
	/** The text area_2. */
	public static JTextArea textArea_2;
	
	/** The E history. */
	private History EHistory;
	
	/** The C history. */
	private HistoryControl CHistory;

	private Customer ECustomer;
		
	public static JTextArea textArea = new JTextArea();
	
	/** The combo box. */

	private JComboBox<String> comboBox;
	
	/** The Duration calllabel. */
	private JLabel DurationCalllabel;
	
	/** The text field. */
	private JTextField textField;
	
	/** The lbluse patternmmssdd. */
	private JLabel lblusePatternmmssdd;
	
	/** The s. */
	public String s=null;
	
	/** The p. */
	public String p=null;
	
	/** The j. */
	public int j=0;
	private JLabel OrderLabel2;
	private JLabel Kitchenlabel;
	private JLabel Crockerylabel;
	private JLabel Picniclabel;
	private JComboBox<String> Kitchen_comboBox;
	private JComboBox<String> Crockery_comboBox;
	private JComboBox<String> Picnic_comboBox;
;	
	
	/**
	 * Instantiates a new update history.
	 */
	public UpdateHistory()
	{
		super();
		SubmitDetails();
	}

	/**
	 * Submit details.
	 */
	public void SubmitDetails(){
		this.setSize( 1500,765 ); //set Frame size
		this.setLayout(null); //set layout
		OrderLabel = new JLabel();     //set new label
		OrderLabel.setBounds(new Rectangle(273, 123, 283, 19));//set label's  size and location
		OrderLabel.setText("Please insert contacting event details:");
		OrderLabel.setFont(new Font("Arial", Font.ITALIC,16));
		CustomerIDLabel = new JLabel();     //set new label
		CustomerIDLabel.setBounds(new Rectangle(295, 201, 113, 19));//set label's  size and location
		CustomerIDLabel.setText("Customer ID:");
		CustomerIDLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		DealIDLabel = new JLabel();     //set new label
		DealIDLabel.setBounds(new Rectangle(295, 229, 57, 18));//set label's  size and location
		DealIDLabel.setText("Deal ID:"); 
		DealIDLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		DateLabel = new JLabel();     //set new label
		DateLabel.setBounds(new Rectangle(295, 172, 113, 18));//set label's  size and location
		DateLabel.setText("Date:");   
		DateLabel.setFont(new Font("Arial", Font.ITALIC,12)); //set label font and size
		DocumentationLabel = new JLabel();     //set new label
		DocumentationLabel.setBounds(new Rectangle(295, 379, 143, 18));//set label's  size and location
		DocumentationLabel.setText("Documentation:");  
		DocumentationLabel.setFont(new Font("Arial", Font.ITALIC,12));
		this.add(CustomerIDLabel, null);   //add label to the panel
		this.add(DealIDLabel, null);   //add label to the panel
		this.add(DateLabel, null);   //add label to the panel
		this.add(DocumentationLabel, null);
		this.add(OrderLabel, null);
		backButton.setBounds(651, 568, 150, 23);   //set button's size and location
		add(backButton);
		
		CustomerIDTextField = new JTextField(); 
		CustomerIDTextField.setBounds(new Rectangle(410, 200, 127, 17)); //set field's  size and location
		this.add(CustomerIDTextField, null);   // add the field to the panel
		
		DealIDTextField = new JTextField();
		DealIDTextField.setBounds(new Rectangle(410, 230, 127, 17)); //set field's  size and location
		this.add(DealIDTextField, null);
	
		final JDateChooser calendarButton1 = new JDateChooser();
		calendarButton1.getSpinner().addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent arg0) {
				java.sql.Date sqldate = new java.sql.Date(calendarButton1.getDate().getTime());
			    s=sqldate.toString();
			  
			}
		});		
		calendarButton1.setBackground(SystemColor.inactiveCaption);
		calendarButton1.setBounds(410, 170, 200, 20);
	
	    this.add(calendarButton1);
	   
 
	   
	    final JTimeChooser timeButton1 = new JTimeChooser();

	    timeButton1.setBackground(SystemColor.inactiveCaption);
		timeButton1.setBounds(550, 336, 200, 20);
	
	    this.add(timeButton1);
		 
		OrderLabel2 = new JLabel("Update Customer's permissions:");
		OrderLabel2.setFont(new Font("Arial", Font.ITALIC, 14));
		OrderLabel2.setBounds(769, 148, 216, 14);
		add(OrderLabel2);
		
		Kitchenlabel = new JLabel();
		Kitchenlabel.setText("Kitchen:");
		Kitchenlabel.setFont(new Font("Arial", Font.ITALIC, 12));
		Kitchenlabel.setBounds(new Rectangle(450, 200, 113, 19));
		Kitchenlabel.setBounds(814, 201, 113, 19);
		add(Kitchenlabel);
		
		Crockerylabel = new JLabel();
		Crockerylabel.setText("Crockery:");
		Crockerylabel.setFont(new Font("Arial", Font.ITALIC, 12));
		Crockerylabel.setBounds(new Rectangle(450, 200, 113, 19));
		Crockerylabel.setBounds(814, 258, 113, 19);
		add(Crockerylabel);
		
		Picniclabel = new JLabel();
		Picniclabel.setText("Picnic:");
		Picniclabel.setFont(new Font("Arial", Font.ITALIC, 12));
		Picniclabel.setBounds(new Rectangle(450, 200, 113, 19));
		Picniclabel.setBounds(814, 315, 113, 19);
		add(Picniclabel);
		
		Kitchen_comboBox = new JComboBox<String>();
		Kitchen_comboBox.setBounds(937, 201, 94, 20);
		Kitchen_comboBox.addItem("");
		Kitchen_comboBox.addItem("Yes");
		Kitchen_comboBox.addItem("No");
		add(Kitchen_comboBox);
		
		Crockery_comboBox = new JComboBox<String>();
		Crockery_comboBox.setBounds(937, 258, 94, 20);
		Crockery_comboBox.addItem("");
		Crockery_comboBox.addItem("Yes");
		Crockery_comboBox.addItem("No");
		add(Crockery_comboBox);
		
		Picnic_comboBox = new JComboBox<String>();
		Picnic_comboBox.setBounds(937, 317, 94, 20);
		Picnic_comboBox.addItem("");
		Picnic_comboBox.addItem("Yes");
		Picnic_comboBox.addItem("No");
		add(Picnic_comboBox);   
		    
		 Addbutton = new JButton("Save");
			Addbutton.addActionListener(new ActionListener() {
				@SuppressWarnings("deprecation")
				public void actionPerformed(ActionEvent arg0) {
					if(!(s==null)){
						textField.setText(timeButton1.getFormatedTime());
						EHistory=new History();
						EHistory.setCId(CustomerIDTextField.getText());
						EHistory.setDId(DealIDTextField.getText());
						EHistory.setHDate(s);
						EHistory.setDocumentation(textArea_2.getText());
						EHistory.setDecision((String)comboBox.getSelectedItem());	
						EHistory.setDuringCall(textField.getText());
						ECustomer=new Customer();
						ECustomer.setPermKitchen((String)Kitchen_comboBox.getSelectedItem());
						ECustomer.setPermCrockery((String)Crockery_comboBox.getSelectedItem());
						ECustomer.setPermPicnic((String)Picnic_comboBox.getSelectedItem());
						CHistory=new HistoryControl();
						if(CHistory.CheckDetails(EHistory.getCId(),EHistory.getDId(),EHistory.getHDate(),EHistory.getDocumentation(),EHistory.getDecision(),ECustomer.getPermKitchen(),ECustomer.getPermCrockery(),ECustomer.getPermPicnic()))
						{
							CHistory.submitEvent(EHistory.getCId(),EHistory.getDId(),EHistory.getHDate(),EHistory.getDocumentation(),EHistory.getDecision(),EHistory.getDuringCall(),ECustomer.getPermKitchen(),ECustomer.getPermCrockery(),ECustomer.getPermPicnic());
							confirmationLable.setVisible(true);
						}
						 else{ 
							 confirmationLable.setVisible(true);
							 confirmationLable.setText("Please fill all the fields!!");
						 }
					}
					else {
						confirmationLable.setVisible(true);
						confirmationLable.setText("Please Choose Date !");
					}
				}
			});
			
			Addbutton.setBounds(491, 568, 150, 23);      //set button size
			add(Addbutton);
			
			confirmationLable = new JLabel("");           // set result label
			confirmationLable.setBounds(480, 602, 350, 85);  // set label's size and location
			add(confirmationLable);                        // add label to the panel
			confirmationLable.setVisible(false);
			
			textArea_2 = new JTextArea();
			textArea_2.setBounds(295, 408, 380, 130);
			add(textArea_2);
			
			comboBox = new JComboBox<String>();
			comboBox.setBounds(410, 258, 127, 20);
			comboBox.addItem("");
			comboBox.addItem("Accept");
			comboBox.addItem("Reject");
			add(comboBox);
			
			JLabel DecisionLabel = new JLabel("Customer Decision:");
			DecisionLabel.setFont(new Font("Arial", Font.ITALIC, 12));
			DecisionLabel.setBounds(295, 260, 113, 14);
			add(DecisionLabel);
			
			DurationCalllabel = new JLabel();
			DurationCalllabel.setText("Duration Call:");
			DurationCalllabel.setFont(new Font("Arial", Font.ITALIC, 12));
			DurationCalllabel.setBounds(new Rectangle(450, 260, 113, 18));
			DurationCalllabel.setBounds(295, 336, 113, 18);
			add(DurationCalllabel);
			
			textField = new JTextField();
			textField.setEditable(false);
			textField.setBounds(new Rectangle(590, 283, 127, 17));
			textField.setBounds(410, 339, 113, 17);
			add(textField);
			
			lblusePatternmmssdd = new JLabel("(Use pattern: \"HH:MM:SS\")");
			lblusePatternmmssdd.setBounds(558, 320, 174, 14);
			add(lblusePatternmmssdd);
			
			JButton btnPresentList = new JButton("Present list");
			btnPresentList.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent arg0) {
				
					textArea.setText(ContactCustomer.preslist);
					textArea.setVisible(true);
				}
			});
			btnPresentList.setBounds(35, 145, 148, 23);
			add(btnPresentList);
			
			textArea.setEditable(false);
			textArea.setBounds(20, 176, 231, 292);
			add(textArea);
			
		
		}
	}
